<?php

/**
 * CategoryModel
 *
 * @author Nguyen Tuan Anh
 * @version
 */

require_once 'Zend/Db/Table/Abstract.php';

class CategoryModel extends Zend_Db_Table_Abstract {
	/**
	 * The default table name
	 */
	protected $_name = 'eway_category';
	protected $_dbAdapter;

	public function getAll() {
		$this->_dbAdapter = Zend_Registry::get ( Constant::DB_ADAPTER );
		$sql = "SELECT cat.* FROM " . $this->_name ." cat ORDER BY cat_position ASC";
		$result = $this->_dbAdapter->fetchAll ( $sql );
		return $result;
	}

	public function insertCat($category = array()) {
		$this->_dbAdapter = Zend_Registry::get ( Constant::DB_ADAPTER );
		$this->_dbAdapter->insert($this->_name, $category);
		$id = $this->_dbAdapter->lastInsertId ();
		return $id;
	}
	
	public function getAllCategoryBySectionId($section_id) {
		$this->_dbAdapter = Zend_Registry::get ( Constant::DB_ADAPTER );
		$sql = "SELECT cat.* FROM " . $this->_name ." cat WHERE cat.sec_id=? ORDER BY cat_position ASC";
		$result = $this->_dbAdapter->fetchAll ( $sql, $section_id );
		return $result;
	}

	public function getAllSectionByCatId($cat_id){
		$this->_dbAdapter = Zend_Registry::get ( Constant::DB_ADAPTER );
		$sql = "SELECT DISTINCT(sec_id) FROM eway_category WHERE cat_id IN $cat_id";
		$result = $this->_dbAdapter->fetchAll ( $sql );
		return $result;
	}
}
